#ifndef TRACKHANDLE_H
#define TRACKHANDLE_H

#include "TString.h"
#include "TVector3.h"
#include "TTree.h"
#include "TChain.h"
#include "TH2F.h"

#include <cstdio>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>

#include "trackStruct.h"
#include "SimEvent.h"

const int EffNX = 100;
const int EffNY = 100;

class TrackHandle {
    private:
        int ADC_Threshold;
        // myTrack track;
        // vector<myTrack> Vtrack;

        int track_w, track_h, contour_size;
        int preADC = 0; 
        int numEvent = 0;
        int preHit = 0;
        int numFrame = 0;
        int preData[EffNX][EffNY] = {0};
        TString currentFileName;

        cv::Mat Array2Mat(TH2F* hist, uint32_t w, uint32_t h);
        cv::Mat S16C1_to_U8C1(cv::Mat S16C1_Array);
        cv::Mat RoiCenter(cv::Mat frame, bool isSource = false);
        int Max_Value(cv::Mat frame);
        coordinate Max_Coor(cv::Mat frame);
        int Sum_Value(cv::Mat frame);
        int Count_NonZero(cv::Mat frame);
        double Diagonal_Length(int x, int y);
        double Ratio_XY(int x, int y);
        coordinate Bcenter_Coor(cv::Mat frame);
        double Dis_Bcenter_Max(coordinate max_coor, coordinate bcenter_coor);
        cv::Mat Hit_Frame(cv::Mat frame);
        int Contour_Size();
        int Length(cv::Mat frame_center);
        std::pair<double, double> truthPixel(TVector3 &truthPosition);

        void feature_extraction(TTree* tree, TTree* truthTree, TTree* outTree, TTree* simTree);

    public:
        TrackHandle(int Threshold);
        ~TrackHandle();

        TTree* calculate(TString filename, TString treeName, TString simFile);
};

#endif